package com.play.airfight.managers;

import android.content.Context;
import android.graphics.Canvas;

import com.play.airfight.enemy.enemy_contract.EnemyTypeData;

import com.play.airfight.enemy.enemy_contract.EnemyFactory;

import com.play.airfight.enemy.enemy_contract.EnemyType;
import com.play.airfight.enemy.enemy_contract.BaseEnemy;

import java.util.ArrayList;
import java.util.Iterator;

public class EnemyManager {

    private ArrayList<BaseEnemy> enemyList = new ArrayList<BaseEnemy>();
    private int[][] enemyMap; // 敌人的出场顺序图 [第几波][数量]
    private int mCurLine = 0;
    private int enemyCounter = 0;    // 总共敌人的数量
    private Context mContext;
    private EnemyFactory enemyFactory = null;
    private BulletsManager mBulletsMgr = null;
    private ExplodeManager mExplodeMgr = null;
    private Player mPlayer = null;
    private boolean isGameStart = true;

    LevelCounter levelCounter;


    public EnemyManager(Context mContext) {
        this.mContext = mContext;
        enemyFactory = new EnemyFactory(mContext);
        enemyMap = GameStage.getLevelOneEnemyMap();
        levelCounter = new LevelCounter(enemyMap);

    }

    public void drawFrame(Canvas canvas) {
        Iterator<BaseEnemy> itor = enemyList.iterator();

        while (itor.hasNext()) {
            BaseEnemy b = itor.next();
            b.drawFrame(canvas);
        }
    }

    public void execLogic() {
        execAddEnemyLogic();   // 添加enemy的地方！

        Iterator<BaseEnemy> itor = enemyList.iterator(); // 迭代器
        while (itor.hasNext()) {
            BaseEnemy b = itor.next();
            b.execLogic();  // 执行每个enemy的execLogic。
        }

        // EnemyManager还需要负责清理“已死亡”的enemy
        itor = enemyList.iterator();
        while (itor.hasNext()) {
            BaseEnemy b = itor.next();
            if (b.isDead()) {
                itor.remove();
                enemyCounter--;
                if (enemyCounter <= 0) {
                    levelCounter.periodIsBattling = false;
                    enemyCounter = 0;
                }
            }
        }
    }

    private void execAddEnemyLogic() {
        // 增加敌人的逻辑

        // 这波是否正在进行
        if (levelCounter.periodIsBattling) {
            return;
        }
        //  结束了 波数 +1，重置状态
        if (enemyCounter == 0) {
            if (isGameStart) {
                // 游戏刚开始是阻止 +1
                isGameStart = false;
            } else {
                levelCounter.levePeriod++;
            }
            levelCounter.periodIsBattling = false;
        } else {
            return;
        }

        // 本关波数是否全部结束
        if (levelCounter.getLevelIsFinish()) {
            //重置状态
            isGameStart = true;
            levelCounter.resetBattleStatu();
            // 切换到下一关

        }

        // 加载这一波所有的敌人
        for (int i = 0; i < enemyMap[levelCounter.levePeriod].length; i++) {
            if (enemyMap[levelCounter.levePeriod][i] == 1) {
                // 加载小兵
                enemyFactory.setEnemyTypeData(new EnemyTypeData(i));
                enemyList.add(enemyFactory.getEnmey(EnemyType.soldier_level_one));
                enemyCounter++;
            } else if (enemyMap[levelCounter.levePeriod][i] == 2) {
                // 加载boss
                enemyFactory.setEnemyTypeData(new EnemyTypeData(i));
                enemyList.add(enemyFactory.getEnmey(EnemyType.boss_level_one));
                enemyCounter++;
            } else {
                // do nothing
            }

        }
        levelCounter.periodIsBattling = true;
    }

    private class LevelCounter {
        private int levePeriod = 0;  // 当前关卡的第几波 对应 下标
        private boolean periodIsBattling = false; // 这一波是否在进行
        private int levelPeriodEnemyCount = 0;  // 这一波敌人的数量

        private int attackWave = 0; // 总共敌人波数

        // 当前波数和总共波数一样表示关卡结束
        private boolean getLevelIsFinish() {
            return levePeriod == attackWave;
        }

        // 重置游戏进行的状态
        private void resetBattleStatu() {
            levePeriod = 0;
            periodIsBattling = false;
            attackWave = 0;
        }

        public LevelCounter(int[][] enemyMap) {
            if (enemyFactory == null) {
                return;
            }
            attackWave = enemyMap.length;
        }
    }




}
